# encoding: utf-8
from __future__ import unicode_literals

from django import forms
from django.contrib.admin import widgets
from django_select2.forms import Select2Widget, Select2MultipleWidget
from easy_select2 import Select2Multiple

from .models import Port


class PortInlineInlineForm(forms.ModelForm):
    class Meta:
        model = Port
        widgets = {
            'users': Select2Multiple(select2attrs={'width': '120px'}, attrs={'data-placeholder': u'选择用户(可多选)'}),
        }
        fields = '__all__'


class AdvancedSearchForm(forms.Form):
    ONE_CHOICES = (('0', u'分开显示'), ('1', u'合并显示'))
    DIRECT_CHOICES = (('2', u'流入'), ('1', u'流出'))
    KMG_CHOICES = (('1000', u'1000'), ('1024', u'1024'))
    CHART_CHOICES = (('0', 'highchart'), ('1', 'echart'))
    ports = forms.ModelMultipleChoiceField(label=u'端口', required=False,
                                           queryset=Port.objects.filter(host__is_active=True, is_active=True),
                                           widget=Select2MultipleWidget(
                                               attrs={'data-placeholder': u'选择端口(可多选)', 'style': 'width:280px'}))
    started = forms.DateField(label=u'开始日期', required=False,
                              widget=widgets.AdminDateWidget(attrs={'placeholder': u'开始日期'}))
    expired = forms.DateField(label=u'结束日期', required=False,
                              widget=widgets.AdminDateWidget(attrs={'placeholder': u'结束日期'}))
    one = forms.ChoiceField(label=u'显示方式', required=False, choices=ONE_CHOICES, widget=Select2Widget(
        attrs={'data-allow-clear': 'false', 'data-minimum-Results-For-Search': 'Infinity', 'style': 'width:90px'}))
    direct = forms.ChoiceField(label=u'计费方向', required=False, choices=DIRECT_CHOICES, widget=Select2Widget(
        attrs={'data-allow-clear': 'false', 'data-minimum-Results-For-Search': 'Infinity', 'style': 'width:90px'}))
    #kmg = forms.ChoiceField(label=u'计费基数', required=False, choices=KMG_CHOICES, widget=Select2Widget(
    #    attrs={'data-allow-clear': 'false', 'data-minimum-Results-For-Search': 'Infinity', 'style': 'width:90px'}))
    kmg = forms.IntegerField(label=u'计费基数', required=False)
    chart = forms.ChoiceField(label=u'图表类型', required=False, choices=CHART_CHOICES, widget=Select2Widget(
        attrs={'data-allow-clear': 'false', 'data-minimum-Results-For-Search': 'Infinity', 'style': 'width:90px'}))

    def __init__(self, *args, **kwargs):
        super(AdvancedSearchForm, self).__init__(*args, **kwargs)
        if 'direct' not in self.initial:  # and self.initial['one'] == -1:
            # del self.fields['one']
            # del self.fields['kmg']
            del self.fields['direct']
            del self.fields['chart']
        if self.initial['user']:
            del self.fields['kmg']
            self.fields['ports'].queryset = Port.objects.filter(host__is_active=True, is_active=True,
                                                                users__in=self.initial['user'])

    def clean(self):
        started = self.cleaned_data.get('started')
        expired = self.cleaned_data.get('expired')
        ports = self.cleaned_data.get('ports')
        one = self.cleaned_data.get('one')
        direct = self.cleaned_data.get('direct')
        kmg = self.cleaned_data.get('kmg')
        chart = self.cleaned_data.get('chart')
        if started > expired:
            raise forms.ValidationError(u'开始时间不能大于结束时间')
        return self.cleaned_data

    class Media:
        css = {'all': ('admin/css/widgets.css',)}
